﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PMQLCHH.BUS;
using PMQLCHH.DTO;
using System.IO;

namespace PMQLCHH.GUI
{
    public partial class THONGKE : DevComponents.DotNetBar.Office2007Form
    {
        HoaDonBUS hd = new HoaDonBUS();

        public DataTable dt_hoadon;

        public THONGKE()
        {
            InitializeComponent();
        }

        private void THONGKE_Load(object sender, EventArgs e)
        {
            ClearControl();

            DataTable dt_mh = MatHangBUS.Load_BaoCao_MatHang();
            if (dt_mh.Rows.Count > 0)
            {
                dtg_mathang.AutoGenerateColumns = false;
                dtg_mathang.DataSource = dt_mh;
            }

        }

        private void btn_baocao_Click(object sender, EventArgs e)
        {
            dt_hoadon = HoaDonBUS.LayDanhSachHoaDonTheoNgay(dtp_tungay.Text, dtp_denngay.Text);
            if (dt_hoadon.Rows.Count > 0)
            {
                int so_mh = 0;
                double tongtien = 0;

                for (int i = 0; i < dt_hoadon.Rows.Count; i++)
                {
                    so_mh += int.Parse(dt_hoadon.Rows[i]["SoLuong"].ToString());
                    tongtien += double.Parse(dt_hoadon.Rows[i]["TongTien"].ToString());
                }

                lbl_khachhang.Text = dt_hoadon.Rows.Count.ToString() + " người";
                lbl_mathang.Text = so_mh.ToString() + " mặt hàng";
                lbl_tongtien.Text = String.Format("{0:0,0} vnđ", double.Parse(tongtien.ToString()));

                dtg_hoadon.AutoGenerateColumns = false;
                dtg_hoadon.DataSource = dt_hoadon;

            }

        }

        private void btn_xuatdl_Click(object sender, EventArgs e)
        {

            if (dtg_hoadon.Rows.Count > 0)
            {
                Export_Excel_BaoCao(dt_hoadon);
            }
            else
            {
                MessageBox.Show("Chưa có dữ liệu");
            }
        }

        public void Export_Excel_BaoCao(DataTable dt)
        {
            try
            {
                SaveFileDialog f = new SaveFileDialog();
                f.Filter = "Excel WorkBook 97-2003 |*.xls";
                if (f.ShowDialog() == DialogResult.OK)
                {

                    Microsoft.Office.Interop.Excel.Application xlApp;
                    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheetChart;
                    object misValue = System.Reflection.Missing.Value;

                    xlApp = new Microsoft.Office.Interop.Excel.Application();
                    xlWorkBook = xlApp.Workbooks.Add(misValue);
                    xlWorkSheetChart = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                    //Isert DL vao excel
                    Insert_DL_BaoCao(xlWorkSheetChart,dt);

                    //Luu file vao temp tam 
                    string file = f.FileName + ".xls";
                    xlWorkBook.SaveAs(file, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                    xlWorkBook.Close(true, misValue, misValue);

                    xlApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

                    MessageBox.Show("Xuất báo cáo thành công");
                }
            }
            catch
            {
                MessageBox.Show("Xuat báo cáo thất bại");
            }
           
        }

        public void Insert_DL_BaoCao(Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet,DataTable dt)
        {

            // Ngày báo cáo
            xlWorkSheet.Cells[1, 1] = "Ngày báo cáo: " + DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
            ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
            ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]).Font.Bold = FontStyle.Bold;
            ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1]).Font.Name = "Times New Roman";

            // Tieu de
            xlWorkSheet.get_Range("A2", "F2").Merge(Type.Missing);
            xlWorkSheet.get_Range("A2", "F2").Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);

            xlWorkSheet.Cells[2, 1] = "BÁO CÁO THỐNG KÊ";
            ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1]).Font.Size = 15;
            ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1]).Font.Bold = FontStyle.Bold;
            ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[2, 1]).Font.Name = "Times New Roman";

            //Tao tieu de cot 

            xlWorkSheet.get_Range("A3", "F" + (dt.Rows.Count + 3).ToString()).Font.Name = "Times New Roman";
            xlWorkSheet.get_Range("A3", "F" + (dt.Rows.Count + 3).ToString()).Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
            xlWorkSheet.get_Range("A3", "F" + (dt.Rows.Count + 3).ToString()).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            xlWorkSheet.Cells[3, 1] = "STT";
            ((Excel.Range)xlWorkSheet.Cells[3, 1]).Font.Bold = FontStyle.Bold;

            xlWorkSheet.Cells[3, 2] = "Mã Hóa Đơn";
            ((Excel.Range)xlWorkSheet.Cells[3, 2]).Font.Bold = FontStyle.Bold;


            xlWorkSheet.Cells[3, 3] = "Tên Khách Hàng";
            ((Excel.Range)xlWorkSheet.Cells[3, 3]).Font.Bold = FontStyle.Bold;
            ((Excel.Range)xlWorkSheet.Cells[3, 3]).EntireColumn.ColumnWidth = 25;

            xlWorkSheet.Cells[3, 4] = "Số Lượng Mua";
            ((Excel.Range)xlWorkSheet.Cells[3, 4]).Font.Bold = FontStyle.Bold;


            xlWorkSheet.Cells[3, 5] = "Tổng Tiền";
            ((Excel.Range)xlWorkSheet.Cells[3, 5]).Font.Bold = FontStyle.Bold;


            xlWorkSheet.Cells[3, 6] = "Thanh Toán";
            ((Excel.Range)xlWorkSheet.Cells[3, 6]).Font.Bold = FontStyle.Bold;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                xlWorkSheet.Cells[i + 4, 1] = (i + 1).ToString();
                xlWorkSheet.Cells[i + 4, 2] = dt.Rows[i]["MaHoaDon"].ToString();
                xlWorkSheet.Cells[i + 4, 3] = dt.Rows[i]["TenKhachHang"].ToString();
                xlWorkSheet.Cells[i + 4, 4] = dt.Rows[i]["SoLuong"].ToString();
                xlWorkSheet.Cells[i + 4, 5] = dt.Rows[i]["TongTien"].ToString();
                if (dt.Rows[i]["TrangThai"].ToString() == "1")
                {
                    xlWorkSheet.Cells[i + 4, 6] = "Đã thanh toán";
                }
                else
                {
                    xlWorkSheet.Cells[i + 4, 6] = "Chưa thanh toán";
                }
               
            }

        }


        public void ClearControl()
        {
            dtp_denngay.Text = DateTime.Now.ToString();
            dtp_tungay.Text = DateTime.Now.ToString();

            lbl_khachhang.Text = "";
            lbl_mathang.Text = "";
            lbl_tongtien.Text = "";
        }



    }
}